package com.zwps.common.core.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zwps.common.core.context.RabbitmqDefine;
import com.zwps.common.core.enums.MessageTypeEnum;
import com.zwps.common.core.rabbitmq.message.LogMessage;
import com.zwps.common.tool.function.FunVoidT2;
import com.zwps.extra.http.LogAPIMessage;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@Slf4j
public class LogService {

    /*@Autowired(required = false)
    private RabbitTemplate rabbitTemplate;*/

    @Autowired
    private ObjectMapper objectMapper;

    /*public void consumeRecord(FunVoidT2<String, LogMessage> fun, Message message, MessageTypeEnum enums) {
        if (rabbitTemplate == null) {
            return;
        }
        String body = null;
        LogMessage logMessage = new LogMessage();
        try {
            body = new String(message.getBody());
            MessageProperties properties = message.getMessageProperties();
            logMessage.setMessageContent(body);
            logMessage.setExchange(properties.getReceivedExchange());
            logMessage.setRouteKey(properties.getReceivedRoutingKey());
            logMessage.setQueue(properties.getConsumerQueue());
            logMessage.setMessageType(enums.getCode());
            logMessage.setMessageSource(2);
            fun.apply(body, logMessage);
            logMessage.setProcessStatus(0);
            sendLogMessage(logMessage);
        } catch (Exception e) {
            log.error("Unexpected error in ", e);
            try {
                String err;
                if (e.getCause() != null && e.getCause().getMessage() != null) {
                    err = e.getCause().getMessage();
                } else {
                    err = e.getMessage();
                }
                logMessage.setProcessStatus(1);
                logMessage.setProcessException(err);
                sendLogMessage(logMessage);
            } catch (Exception e2) {
                log.error("Record error log, Unexpected error in ", e2);
            }
        }

    }*/

    public void sendLogMessage(LogAPIMessage logMessage) {
       /* if (rabbitTemplate == null) {
            return;
        }
        try {
            String message = objectMapper.writeValueAsString(logMessage);
            CorrelationData correlationData = new CorrelationData();
            rabbitTemplate.convertAndSend(RabbitmqDefine.EXCHANGE_NAME, RabbitmqDefine.LOG_API_ROUTE, message,
                    correlationData);
        } catch (Exception e) {
            log.error("Unexpected error in ", e);
        }*/
    }
    
    private void sendLogMessage(LogMessage logMessage) {
        /*try {
            String message = objectMapper.writeValueAsString(logMessage);
            CorrelationData correlationData = new CorrelationData();
            rabbitTemplate.convertAndSend(RabbitmqDefine.EXCHANGE_NAME, RabbitmqDefine.LOG_MESSAGE_ROUTE, message,
                    correlationData);
        } catch (Exception e) {
            log.error("Unexpected error in ", e);
        }*/
    }
}
